#include "RingQueue.hpp"
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <ctime>
using namespace std;

struct ThreadData
{
    string thread_name;
    RingQueue<int>* rq;
};
void* Productor(void* args)
{
    //RingQueue<int>* rq = static_cast<RingQueue<int>*>(args);
    ThreadData* td = static_cast<ThreadData*>(args);
    RingQueue<int>* rq = td->rq;
    
    while(true)
    {
        int data = rand()%10 + 1;
        usleep(10);
        printf("%s produce a data: %d\n",td->thread_name.c_str(),data);
        rq->Push(data);
        sleep(1);
    }
    return nullptr;
}
void* Consumer(void* args)
{
    //RingQueue<int>* rq = static_cast<RingQueue<int>*>(args);
    ThreadData* td = static_cast<ThreadData*>(args);
    RingQueue<int>* rq = td->rq;
    while(true)
    {
        int data;
        rq->Pop(&data);
        printf("%s get a data: %d\n",td->thread_name.c_str(),data);
    }
    return nullptr;
}
int main()
{
    srand(time(nullptr)^getpid());
    pthread_t c[5],p[3];
    //RingQueue<int> rq;
    RingQueue<int>* rq = new RingQueue<int>();
    for(int i = 0; i < 3; i++)
    {
        ThreadData* data = new ThreadData();
        data->thread_name = "Productor " + to_string(i);
        data->rq = rq;
        pthread_create(&p[i],nullptr,Productor,data);
    }
    for(int i = 0; i < 5; i++)
    {
        ThreadData* data = new ThreadData();
        data->thread_name = "Consumer " + to_string(i);
        data->rq = rq;
        pthread_create(&c[i],nullptr,Consumer,data);
    }
    for(int i = 0; i < 3; i++)
    {
        pthread_join(p[i],nullptr);
    }
    for(int i = 0; i < 5; i++)
    {
        pthread_join(c[i],nullptr);
    }
    return 0;
}